package com.aaa.hospital.Configurationterminal.controller;

import com.aaa.hospital.Client.service.VisitingcardService;
import com.aaa.hospital.ManagementEnd.pojo.User;
import com.aaa.hospital.ManagementEnd.service.UserServices;
import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/register")
public class RegisterController {


    @Autowired
private UserServices  userServices;

    @Autowired
    private VisitingcardService visitingcardService;
    //短信平台相关参数
    //这个不用改
    private String apiUrl = "https://sms_developer.zhenzikj.com";
    //榛子云系统上获取
    private String appId = "109203";
    private String appSecret = "9cbe85e1-fe2a-4245-8806-b39dae608513";
@RequestMapping("/registerCode")
@ResponseBody
public int phone_sole(String phone){
        try {
            JSONObject json = null;
            //    //随机生成验证码
            String code = String.valueOf(new Random().nextInt(999999));
            //将验证码通过榛子云接口发送至手机
            ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
            Map<String, Object> params = new HashMap<String, Object>();
            //前台输入的手机号
            params.put("number", phone);
            //这个模板id对应的是榛子云个人中心的模板id
            params.put("templateId", 5509);
            String[] templateParams = new String[2];
            templateParams[0] = code;
            templateParams[1] = "5分钟";
            params.put("templateParams", templateParams);
            String result = client.send(params);
            System.out.println(result);
            json = JSONObject.parseObject(result);
            if (json.getIntValue("code")!=0){//发送短信失败
                return 1;
            }
            int a=Integer.parseInt(code);
            return a;
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
}



    //查询手机号是否唯一
    @RequestMapping("/phone_sole")
    @ResponseBody
    public boolean findIsTel(String phone){
        User user = userServices.user_phone(phone);
        if (user == null) {
            return true;
        }
        return false;
    }




//用户添加
@RequestMapping("/user_add")
@ResponseBody
public int user_add(String uname, String idcard, String phone, String birthday, String sex, String password) throws ParseException {
    System.out.println(uname + "<==>" +
            idcard + "<==>" +
            phone + "<==>" +
            birthday + "<==>" +
            sex + "<==>" +
            password + "<==>");
    DateFormat dat=new SimpleDateFormat("yyyy-MM-dd");
        Date date=dat.parse(birthday);
        User user=new User();
        user.setUname(uname);
        user.setIdcard(idcard);
        user.setPhone(phone);
        user.setAddress(null);
        user.setBirthday(date);
        user.setPassword(password);
        user.setSex(sex);
    boolean user_add = userServices.user_add(user);
    if (user_add == true) {
        User user1 = userServices.user_Byidcard(idcard);
        if (user1 != null) {
            int i = visitingcardService.insertBalance(0, user1.getUid());

            if (i >0) {
                return user1.getUid();
            }else {
                return 0;
            }
        }else {
            return 0;
        }
    }
    return 0;

//姓名，身份证号，手机号，地址，出生日期，性别，密码；


}




}
